// pages/shophomepage/shophomepage.js
const allhomepage = require('../../utils/util.js')
import basesup from '~/utils/supplier.js'
import baseuser from '~/utils/users.js'

Page({
    /**
     * 页面的初始数据
     */

    data: {
        leftactiveKey: '',
        leilist: [],

        option1: [
            { text: '全部商品', value: 0 },
            { text: '新款商品', value: 1 },
            { text: '活动商品', value: 2 },
        ],
        value1: 0,
        start: 1,

        companymsg: {},
        companylist: [],
        ismycompany: 1,

        pages: 1,
        update: false,
        startingPrice: 0,
        brokerage: 0,
        startingPrice1: 1,
        brokerage1: 1,
        category:''
    },
    startingPricechange() {
        this.setData({
            "pages": "1",
            update: false,
        })
        if (this.data.startingPrice == 0) {
            this.setData({
                startingPrice: 1,
                startingPrice1: 2,
            })
            this.getcompanydata()

        } else if (this.data.startingPrice == 1) {
            this.setData({
                startingPrice: 2,
                startingPrice1: 1,

            })
            this.getcompanydata()

        } else {
            this.setData({
                startingPrice: 0,
                startingPrice1: 1,

            })
            this.getcompanydata()

        }
    },
    brokeragechange(){
        this.setData({
            "pages": "1",
            update: false,
        })
        if (this.data.brokerage == 0) {
            this.setData({
                brokerage: 1,
                brokerage1: 2
            })
            this.getcompanydata()

        } else if (this.data.brokerage == 1) {
            this.setData({
                brokerage: 2,
                brokerage1: 1

            })
            this.getcompanydata()

        } else {
            this.setData({
                brokerage: 0,
                brokerage1: 2

            })
            this.getcompanydata()

        }
    },
    async getfenlei() {
      let resdata = await baseuser.profirst()
      // console.log('rress',resdata);
      let newdata = resdata.data.result
      for (const i in newdata) {
        newdata[i].text = newdata[i].name
        newdata[i].value = newdata[i].name
      }
      this.setData({
        leilist: newdata
      })
      console.log('newdata', this.data.leilist);

    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },
    updatachange() {
        this.setData({
            update: !this.data.update,
        })
        this.getcompanydata()

    },

    swipch() {
        allhomepage.swiperindex()
    },
    allcompany() {
        this.setData({
            leftactiveKey: '',
        })
        this.setData({
            "pages": "1",
            update: false,
            startingPrice: this.data.startingPrice1,
            brokerage: this.data.brokerage1
        })
        this.getcompanydata()
    },
    nextdetail(e) {
        wx.navigateTo({
            url: `../details/details?id=${e.currentTarget.dataset.id}`,
        })
    },
    bottomstsrt(){
        this.setData({
            "pages": "1",
            update: false,
            startingPrice: 0,
            brokerage: 0
        })
        this.getcompanydata()

    },
    changestart(item) {
        this.setData({
            start: item.currentTarget.dataset.start
        })
    },

    open_dropdown() {
      wx.setPageStyle({
        style: {
          overflow: 'hidden'
        }
      })
      this.setData({
        isShowPopup1: true
      })
    },
    close_dropdown() {
      wx.setPageStyle({
        style: {
          overflow: ''
        }
      })
      this.setData({
        isShowPopup1: false
      })
    },
    dropchange() {
      console.log(11);
      //   this.setData({
      //     category: items.detail
      //   })
        this.getcompanydata()

    },
    async getshangpin() {
        let resdata = await basesup.storeClass()
        let newdata = resdata.data.Result.records
        console.log(resdata);
        for (const i in newdata) {
            newdata[i].text = newdata[i].categoryName
            newdata[i].value = newdata[i].categoryName
        }
        this.setData({
            leilist: newdata
        })
    },

    async getcompanydata() {
      // console.log(this.data.companyid);
        let reqdata = {
            supplierId: this.data.companyid,
            "current": this.data.pages,
            "pageSize": "10",
            "update": "false",
            "startingPrice": 1,
            "brokerage": 1,
            "category": this.data.leftactiveKey
        }
        let resdata = await basesup.companystore(reqdata)
        console.log(resdata.data);
        if (resdata.code == 200) {
            this.setData({
                companymsg: resdata.data.result.store,
                companylist: resdata.data.result.page.records,
               
            })

        } else {
            wx.showToast({
                title: '查询失败',
                icon: 'none',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 2000);
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.getfenlei()

        if (JSON.stringify(options) != undefined && JSON.stringify(options).length > 2) {
            this.setData({
                companyid: options.id
            })
            this.getcompanydata()
            this.getshangpin()
        } else {
            wx.showToast({
                title: '查询失败',
                icon: 'none',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 2000);
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})